package com.cloudcms.web.controller.cms;

import com.cloudcms.web.annotation.SessionAdmin;
import com.cloudcms.web.common.BusinessException;
import com.cloudcms.web.common.Result;
import com.cloudcms.web.entity.Admin;
import com.cloudcms.web.entity.Types;
import com.cloudcms.web.service.TreeService;
import com.cloudcms.web.util.Constants;
import com.cloudcms.web.service.CmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/types")
public class TypesController {
    /**
     * 注入service
     */
    @Autowired
    private CmsService cmsService;

    @Autowired
    private TreeService treeService;

    @RequestMapping(value = "/list/1.0", method = { RequestMethod.GET, RequestMethod.POST })
    public DeferredResult<String>  list(Model model, HttpServletRequest request, HttpServletResponse response){
        String status = request.getParameter("status");
        String parentid = request.getParameter("parentid");
        String id = request.getParameter("id");
        String tree = request.getParameter("tree");

        parentid = StringUtils.isEmpty(parentid)?"1":parentid;
        status = StringUtils.isEmpty(status)?"1":status;
        Types types = new Types();
        types.setStatus(status);
        Result result = new Result();
        if(StringUtils.isEmpty(id)){
            if ("1".equals(tree)){
                result.put("data", treeService.buildTree(cmsService.selectTypes(types),parentid));
            }else{
                types.setParentid(parentid);
                result.put("data",cmsService.selectTypes(types));
            }
        }else{
            types.setId(id);
            result.put("data",cmsService.selectTypes(types));
        }

        DeferredResult<String> deferredResult = new DeferredResult<String>(Constants.TIMEOUT_VALUE);
        deferredResult.setResult(result.toString());
        return deferredResult;
    }
    @RequestMapping(value = "/list/2.0", method = { RequestMethod.GET, RequestMethod.POST })
    public DeferredResult<String> list2(Model model, HttpServletRequest request, HttpServletResponse response,@SessionAdmin Admin admin){
        return  list(model,request,response);
    }
    @RequestMapping(value = "/location/1.0", method = { RequestMethod.GET, RequestMethod.POST })
    public DeferredResult<String> location(Model model, HttpServletRequest request, HttpServletResponse response){
        String typeid = request.getParameter("typeid");
        typeid = StringUtils.isEmpty(typeid)?"1":typeid;
        Types types = new Types();
        types.setStatus("1");

        Result result = new Result();
        result.put("data",treeService.getLocation(typeid, cmsService.selectTypes(types)));
        DeferredResult<String> deferredResult = new DeferredResult<String>(Constants.TIMEOUT_VALUE);
        deferredResult.setResult(result.toString());
        return deferredResult;
    }
    @RequestMapping(value = "/edit/1.0", method = { RequestMethod.GET, RequestMethod.POST })
    public DeferredResult<String>  edit(Model model, HttpServletRequest request, HttpServletResponse response,@SessionAdmin Admin admin){

        String parentid = request.getParameter("parentid");
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        if(StringUtils.isEmpty(id)&&StringUtils.isEmpty(parentid)){
            throw  new BusinessException(701L,"父栏目parentid和栏目id不能都为空");
        }
        Types types = new Types();
        types.setStatus(StringUtils.isEmpty(status)?"1":status);
        types.setName(request.getParameter("name"));
        types.setDescription(request.getParameter("description"));
        types.setLitpic(request.getParameter("litpic"));
        types.setManageurl(request.getParameter("manageurl"));
        types.setUrl(request.getParameter("url"));
        types.setRank(StringUtils.isEmpty(request.getParameter("rank"))?"20":request.getParameter("rank"));
        types.setReservedchar1(request.getParameter("reservedchar1"));
        types.setReservedchar2(request.getParameter("reservedchar2"));
        types.setReservedchar3(request.getParameter("reservedchar3"));

        if(StringUtils.isEmpty(id)){
            types.setParentid(parentid);
            types.setCreateby(admin.getUsername());
            cmsService.insertTypes(types);
        }else{
            types.setId(id);
            cmsService.updateTypes(types);
        }

        Result result = new Result();
        DeferredResult<String> deferredResult = new DeferredResult<String>(Constants.TIMEOUT_VALUE);
        deferredResult.setResult(result.toString());
        return deferredResult;
    }
}